<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="root"></div>
</body>
<script>
  // fetch('https://api.github.com/users/ruanyf')
  //   .then(response => response.json())
  //   .then(json => console.log(json))
  //   .catch(err => console.log('Request Failed', err));

  const byteOrderMarks = [
    {
      encoding: "UTF-EBCDIC",

      regex: new RegExp("221 115 102 115"),
    },

    {
      encoding: "GB-18030",

      regex: new RegExp("132 49 149 51"),
    },

    {
      encoding: "UTF-32LE",

      regex: new RegExp("255 254 0 0"),
    },

    {
      encoding: "UTF-32BE",

      regex: new RegExp("0 0 254 255"),
    },

    {
      encoding: "UTF-8",

      regex: new RegExp("239 187 191"),
    },

    {
      encoding: "UTF-7",

      regex: new RegExp("43 47 118"),
    },

    {
      encoding: "UTF-1",

      regex: new RegExp("247 100 76"),
    },

    {
      encoding: "SCSU",

      regex: new RegExp("14 254 255"),
    },

    {
      encoding: "BOCU-1",

      regex: new RegExp("251 238 40"),
    },

    {
      encoding: "UTF-16BE",

      regex: new RegExp("254 255"),
    },

    {
      encoding: "UTF-16LE",

      regex: new RegExp("255 254"),
    },
  ];

  const checkByteOrderMark = (uInt8Start) => {
    for (const element of byteOrderMarks) {
      if (element.regex.test(uInt8Start)) return element.encoding;
    }

    return null;
  };



  const root = document.getElementById('root')
  async function fetchText (params) {
    const response = await fetch('/测试文本.txt')
    // 第一种
    // const res = await response.text();
    // console.log('res', res);
    // root.append( '结果：' + res)

    // console.log('response', response);

    // 第2种
    const data = await response.blob();

    const reader = new FileReader()
    reader.onload = () => {
      var text = reader.result
      root.append('结果：' + text)
    }
    reader.readAsText(data, 'GBK')

  }
  fetchText();
</script>

</html>